package com.xw.bo.ks.service.impl;

import com.xw.bo.ks.client.WechatClient;
import com.xw.bo.ks.entity.CompanyToken;
import com.xw.bo.ks.kafka.KafkaProducer;
import com.xw.bo.ks.service.HtpRequestService;
import com.xw.bo.ks.service.TokenService;
import com.xw.bo.ks.util.XwUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * @author： XW -> ~
 * @create： 2024/4/1 17:48
 */
@Slf4j
@Service
public class HtpRequestServiceImpl implements HtpRequestService {
    static String url = "https://api.shangboyx.com/api/admin/goods/goods/list?time_sort=2&status=1&page=1&page_size=10";

    @Resource
    private WechatClient wechatClient;

    @Resource
    private KafkaProducer kafkaProducer;

    @Resource
    private TokenService tokenService;

    @Override
    public void htpAll() {

//        htpAnchor();
        htpItem();
        htpOrder();

    }

    @Override
    public void htpOrder() {

        Integer dayTime = XwUtil.getDayTime(System.currentTimeMillis());
        Integer userId = 10000;

        for (int i = 1; i < 315; i++) {
            String url = "https://api.shangboyx.com/api/admin/super/detail/list?end_date=2024-04-07&page="+i+"&page_size=50&start_date=2024-04-07";
            String dsUserId = userId.toString();
            String dsTime = dayTime.toString();
            CompanyToken companyToken = tokenService.getToken(1);
            String dsToken = companyToken.getToken();
            String v = DigestUtils.md5Hex(dsUserId + dsTime + dsTime + url);
            String dsSign = v;

            String message = wechatClient.orderInfo(url, dsSign, userId, dayTime, dsToken);
//            log.info("htpOrder message = {}", message);
            kafkaProducer.sendMessage("myTopic",message);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

    }

    @Override
    public void htpItem() {
        Integer dayTime = XwUtil.getDayTime(System.currentTimeMillis());
//        Integer dayTime = 1712564748;
        Integer userId = 10000;
        for (int i = 1; i < 275; i++) {
            String url = "https://api.shangboyx.com/api/admin/goods/goods/list?page="+i+"&page_size=50&status=1&time_sort=2";
            String dsUserId = userId.toString();
            String dsTime = dayTime.toString();
            CompanyToken companyToken = tokenService.getToken(1);
            String dsToken = companyToken.getToken();
            String v = DigestUtils.md5Hex(dsUserId + dsTime + dsTime + url);
            String dsSign = v;

            String message = wechatClient.orderInfo(url, dsSign, userId, dayTime, dsToken);
//            log.info("htpItem message = {}", message);
            kafkaProducer.sendMessage("goodsInfoKo",message);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

    }

    @Override
    public void htpAnchor() {
        Integer dayTime = XwUtil.getDayTime(System.currentTimeMillis());

        Integer userId = 10000;
        for (int i = 1; i < 64; i++) {
            String url = "https://api.shangboyx.com/api/admin/anchor/archive/list?page="+i+"&page_size=50";
            String dsUserId = userId.toString();
            String dsTime = dayTime.toString();
            CompanyToken companyToken = tokenService.getToken(1);
            String dsToken = companyToken.getToken();
            String v = DigestUtils.md5Hex(dsUserId + dsTime + dsTime + url);
            String dsSign = v;
//            log.info("dsSign = {}", dsSign);
            String message = wechatClient.orderInfo(url, dsSign, userId, dayTime, dsToken);
            log.info("htpAnchor message = {}", message);
            kafkaProducer.sendMessage("anchorInfo",message);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

    }

    public static void main(String[] args) {

        String baseUrl = "https://api.shangboyx.com/api/admin/goods/goods/list?";
        String msg = "start_date=2024-04-07&end_date=2024-04-07&page=\"+i+\"&page_size=50";

        StringBuilder stringBuilder = new StringBuilder(baseUrl);
        stringBuilder
        .append(encoderValue("page")).append("=").append(encoderValue("1")).append("&")
        .append(encoderValue("page_size")).append("=").append(encoderValue("10")).append("&")
        .append(encoderValue("status")).append("=").append(encoderValue("1")).append("&")
        .append(encoderValue("time_sort")).append("=").append(encoderValue("2"));

        String encoderValue = encoderValue("time_sort");
        log.info("encoderValue = {}", encoderValue);

        String msgUrl = stringBuilder.toString();
//        String msgUrl = builderString.substring(0,builderString.length() - 1);
        log.info("msgUrl = {}", msgUrl);

        String url = "https://api.shangboyx.com/api/admin/goods/goods/list?"+encoderValue("time_sort")+"=2&status=1&page=1&page_size=10";

        String dsSign = "1000";
        String dsUserId = "10000";
        String dsTime = "1712573399";
        String dsToken = "start_date";
        String v = DigestUtils.md5Hex(dsUserId + dsTime + dsTime +msgUrl);
        // 38f5bf3613031322c19ef2fa7d5d227b
        log.info("v = {}", v);
    }


    private static String encoderValue(String value){
        try {
            return URLEncoder.encode(value, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }
}
